
#include "mMatrix4x4.h"

mMatrix4x4::mMatrix4x4(void)
{
	Identity();
}

mMatrix4x4::mMatrix4x4(const mMatrix4x4 &a)
{
	m00 = a.m00;
	m01 = a.m01;
	m02 = a.m02;
	m10 = a.m10;
	m11 = a.m11;
	m12 = a.m12;
	m20 = a.m20;
	m21 = a.m21;
	m22 = a.m22;
}

mMatrix4x4::mMatrix4x4(float nm00, float nm01, float nm02, float nm03,
						 float nm10, float nm11, float nm12, float nm13,
						 float nm20, float nm21, float nm22, float nm23,
						 float nm30, float nm31, float nm32, float nm33)
{
	m00 = nm00;
	m01 = nm01;
	m02 = nm02;
	m10 = nm10;
	m11 = nm11;
	m12 = nm12;
	m20 = nm20;
	m21 = nm21;
	m22 = nm22;
}

mMatrix4x4::mMatrix4x4(float m[16])
{
	m00 = m[0];
	m01 = m[1];
	m02 = m[2];
	m10 = m[3];
	m11 = m[4];
	m12 = m[5];
	m20 = m[6];
	m21 = m[7];
	m22 = m[8];
}

mMatrix4x4::mMatrix4x4(float m[4][4])
{
	m00 = m[0][0];
	m01 = m[0][1];
	m02 = m[0][2];
	m10 = m[1][0];
	m11 = m[1][1];
	m12 = m[1][2];
	m20 = m[2][0];
	m21 = m[2][1];
	m22 = m[2][2];
}

mMatrix4x4::~mMatrix4x4(void)
{
}

mMatrix4x4 &mMatrix4x4::operator =(const mMatrix4x4 &a)
{
	m00 = a.m00;
	m01 = a.m01;
	m02 = a.m02;
	m10 = a.m10;
	m11 = a.m11;
	m12 = a.m12;
	m20 = a.m20;
	m21 = a.m21;
	m22 = a.m22;
	return *this;
}

bool mMatrix4x4::operator ==(const mMatrix4x4 &a) const
{
	return m00 == a.m00 && m01 == a.m01 && m02 == a.m02
		&& m10 == a.m10 && m11 == a.m11 && m12 == a.m12
		&& m22 == a.m22 && m21 == a.m21 && m22 == a.m22;
}

bool mMatrix4x4::operator !=(const mMatrix4x4 &a) const
{
	return m00 != a.m00 || m01 != a.m01 || m02 != a.m02
		|| m10 != a.m10 || m11 != a.m11 || m12 != a.m12
		|| m22 != a.m22 || m21 != a.m21 || m22 != a.m22;
}

mMatrix4x4 mMatrix4x4::operator -(void) const
{
	mMatrix4x4 m(*this);
	m.m00 *= -1;
	m.m01 *= -1;
	m.m02 *= -1;
	m.m10 *= -1;
	m.m11 *= -1;
	m.m21 *= -1;
	m.m20 *= -1;
	m.m21 *= -1;
	m.m22 *= -1;
	return m; 
}

mMatrix4x4 mMatrix4x4::operator +(const mMatrix4x4 &a) const
{
	mMatrix4x4 m(*this);
	m.m00 += a.m00;
	m.m01 += a.m01;
	m.m02 += a.m02;
	m.m10 += a.m10;
	m.m11 += a.m11;
	m.m12 += a.m12;
	m.m20 += a.m20;
	m.m21 += a.m21;
	m.m22 += a.m22;
	return m;
}

mMatrix4x4 mMatrix4x4::operator -(const mMatrix4x4 &a) const
{
	mMatrix4x4 m(*this);
	m.m00 -= a.m00;
	m.m01 -= a.m01;
	m.m02 -= a.m02;
	m.m10 -= a.m10;
	m.m11 -= a.m11;
	m.m12 -= a.m12;
	m.m20 -= a.m20;
	m.m21 -= a.m21;
	m.m22 -= a.m22;
	return m;
}

mMatrix4x4 mMatrix4x4::operator *(const mMatrix4x4 &a) const
{
	mMatrix4x4 m;
	m.m00 = (m00 * a.m00) + (m01 * a.m10) + (m02 * a.m20);
	m.m01 = (m00 * a.m01) + (m01 * a.m11) + (m02 * a.m21);
	m.m02 = (m00 * a.m02) + (m01 * a.m12) + (m02 * a.m22);
	m.m10 = (m10 * a.m00) + (m11 * a.m10) + (m12 * a.m20);
	m.m11 = (m10 * a.m01) + (m11 * a.m11) + (m12 * a.m21);
	m.m12 = (m10 * a.m02) + (m11 * a.m12) + (m12 * a.m22);
	m.m20 = (m20 * a.m00) + (m21 * a.m10) + (m22 * a.m20);
	m.m21 = (m20 * a.m01) + (m21 * a.m11) + (m22 * a.m21);
	m.m22 = (m20 * a.m02) + (m21 * a.m12) + (m22 * a.m22);
	return m;
}

mVector4 mMatrix4x4::operator *(const mVector4 &a) const
{
	mVector4 v;
	v.X = (m00 * a.X) + (m01 * a.Y) + (m02 * a.Z);
	v.Y = (m10 * a.X) + (m11 * a.Y) + (m12 * a.Z);
	v.Z = (m20 * a.X) + (m21 * a.Y) + (m22 * a.Z);
	return v;
}

mMatrix4x4 mMatrix4x4::operator *(float a) const
{
	mMatrix4x4 m(*this);
	m.m00 *= a;
	m.m01 *= a;
	m.m02 *= a;
	m.m10 *= a;
	m.m11 *= a;
	m.m21 *= a;
	m.m20 *= a;
	m.m21 *= a;
	m.m22 *= a;
	return m;	
}

mMatrix4x4 &mMatrix4x4::operator +=(const mMatrix4x4 &a)
{
	m00 += a.m00;
	m01 += a.m01;
	m02 += a.m02;
	m10 += a.m10;
	m11 += a.m11;
	m12 += a.m12;
	m20 += a.m20;
	m21 += a.m21;
	m22 += a.m22;
	return *this;
}

mMatrix4x4 &mMatrix4x4::operator -=(const mMatrix4x4 &a)
{
	m00 -= a.m00;
	m01 -= a.m01;
	m02 -= a.m02;
	m10 -= a.m10;
	m11 -= a.m11;
	m12 -= a.m12;
	m20 -= a.m20;
	m21 -= a.m21;
	m22 -= a.m22;
	return *this;
}

mMatrix4x4 &mMatrix4x4::operator *=(float a)
{
	m00 *= a;
	m01 *= a;
	m02 *= a;
	m10 *= a;
	m11 *= a;
	m21 *= a;
	m20 *= a;
	m21 *= a;
	m22 *= a;
	return *this;	
}

float &mMatrix4x4::operator [](int index)
{
	switch (index)
	{
	case 0:
		return m00;
	case 1:
		return m01;
	case 2:
		return m02;
	case 3:
		return m10;
	case 4:
		return m11;
	case 5:
		return m12;
	case 6:
		return m20;
	case 7:
		return m21;
	case 8:
		return m22;
	default:
		return m00;
	}
}

void mMatrix4x4::Identity(void)
{
	m00 = m11 = m22 = 1.0f;
	m01 = m02 = m10 = m12 = m20 = m21 = 0.0f;
}